package mycompany.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpSession;
import mycompany.helper.HashHelper;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 */
public class UserBaseModel {
    
    private String id;
    private HttpSession session;
    private String syntheticAlias;
    
    /**
     * Constructs an instance
     * @param sessionId defines session id 
     */
    public UserBaseModel(HttpSession session){
        this.session = session;
        this.syntheticAlias = generateSyntheticAlias();
        this.id = HashHelper.sha1(session.getId(), syntheticAlias);
    }
    
    /**
     * Constructs an instance
     */
    public UserBaseModel(){
        this.syntheticAlias = generateSyntheticAlias();
        this.id = HashHelper.sha1(""+new Date().getTime(), syntheticAlias);
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public String getId(){
        return id;
    }
    
    public String getName(){
        return syntheticAlias;
    }
    
    public <T extends UserBaseModel> boolean equals(T user){
        return this.id.equals(user.getId());
    }
    
    public boolean isUserSessionIdEquals(String sessionId){
        return session.getId().equals(sessionId);
    }
            
    private String generateSyntheticAlias(){
        List<String> nicknames = new ArrayList<String>();
        nicknames.add("Anonymous");
        nicknames.add("Angry bird");
        nicknames.add("xXx");
        nicknames.add("Person-X");
        nicknames.add("User");
        
        Random random = new Random();
        return nicknames.get(random.nextInt(nicknames.size()))+"-"+random.nextInt(Integer.MAX_VALUE);
    }
}
